let BlackListDao = require('../dao/blackListDao');
let UserDao = require('../dao/userDao');
let FriendDao = require('../dao/friendDao');
let FriendRequestDao = require('../dao/friendRequestDao');
exports.create = function (params) {
    return new Promise(function (resolve, reject) {
        mysqlHelper.getConn()
            .then(function (conn) {
                conn.openTransaction()
                    .then(function () {
                        return FriendDao.isFriend(conn, {
                            openid: params.openid,
                            FriendOpenId: params.BlackOpenId
                        });
                    })
                    .then(function (result) {
                        if (result.length > 0) {
                            params.IsFriend = 'Y';
                        } else {
                            params.IsFriend = 'F';
                        }
                        return BlackListDao.createBlackList(conn, params).then(function () {
                            return FriendDao.blackFriend(conn, {
                                IsBlack:"T",
                                openid: params.openid,
                                FriendOpenId: params.BlackOpenId
                            });
                        });
                    })
                    .then(function () {
                        return FriendRequestDao.deleteAllFriendRequest(conn, {
                            MeOpenId: params.openid,
                            TaOpenId: params.BlackOpenId
                        })
                    })
                    .then(function () {
                        return conn.commitTransaction();
                    }, function (err) {
                        conn.rollbackTransaction();
                        throw err;
                    })
                    .then(function () {
                        conn.releaseConn();
                        resolve("拉黑成功")
                    }, function (err) {
                        console.log(err);
                        conn.releaseConn();
                        reject(err)
                    });
            })

    });
};
exports.list = function (params) {
    return new Promise(function (resolve, reject) {
        UserDao.queryUserDetail(masterPool, params)
            .then(function (result) {
                return BlackListDao.selectBlack(masterPool, {params: params, selfInfo: result[0]})
            })
            .then(resolve, reject)
    });
};
exports.deleteBlack = function (params) {
    return new Promise(function (resolve, reject) {
        let blackResult = {};
        mysqlHelper.getConn()
            .then(function (conn) {
                conn.openTransaction()
                    .then(function () {
                        return BlackListDao.queryBlack(conn, params);
                    })
                    .then(function (result) {
                        blackResult=result[0];
                        if (result[0].IsFriend == 'F') {
                            let createdAt = new Date();
                            return FriendDao.batchInsertFriend(conn, [
                                [params.openid, result[0].BlackOpenId, createdAt],
                                [result[0].BlackOpenId, params.openid, createdAt]
                            ]);
                        }
                    })
                    .then(function () {
                        return FriendDao.blackFriend(conn, {
                            IsBlack:"F",
                            openid: params.openid,
                            FriendOpenId: blackResult.BlackOpenId
                        });
                    })
                    .then(function () {
                        return BlackListDao.deleteBlack(conn, params);
                    })
                    .then(function () {
                        return conn.commitTransaction();
                    }, function (err) {
                        conn.rollbackTransaction();
                        throw err;
                    })
                    .then(function () {
                        conn.releaseConn();
                        resolve("操作成功")
                    }, function (err) {
                        console.log(err);
                        conn.releaseConn();
                        reject(err)
                    });
            })

    });
};